<?php
require($_SERVER['DOCUMENT_ROOT'] . '/setting.php');
function verifyToken($token)
{
    global $redis_pz;
    $redis = new Redis();
    $redis->connect($redis_pz['host'], $redis_pz['port']);
    $redis->auth($redis_pz['password']);
    $data = $redis->get($token);
    if ($data) {
        $data = json_decode($data, true);
        if ($data['timestamp'] > time()) {
            return true;
        }
    }
    return false;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['success' => false, 'message' => 'error: requests are not allowed']);
    exit;
}
require($_SERVER['DOCUMENT_ROOT'] . '/cofd/functions.php');
$data_y = file_get_contents("php://input");
$data_x = json_decode($data_y, true);
$token  = $data_x['token'];
if ($token != '') {
    $token  = encrypt($token, 'D', 'yhsdylpo9342');
} else {
    echo json_encode(['success' => false, 'message' => 'error: invalid token']);
    exit;
}
if (!verifyToken($token)) {
    echo json_encode(['success' => false, 'message' => 'error: invalid token']);
    exit;
}
require($_SERVER['DOCUMENT_ROOT'] . '/cofd/common.php');
$cdk = $data_x['cdk'];
$userid = $data_x['userid'];
$sql = "SELECT * FROM `cdk` WHERE cdk=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $cdk);
if ($stmt->execute()) {
    if ($result = $stmt->get_result()) {
        $row = $result->fetch_assoc();
        if (isset($row['canuse']) && isset($row['used']) && isset($row['vips']) && isset($row['words'])) {
            $canuse = $row['canuse'];
            $used = $row['used'];
            $vips = $row['vips'];
            $words = $row['words'];
        }else{
            echo json_encode(['success' => false, 'message' => 'error: invalid cdk']);
            $stmt->close();
            $conn->close();
            exit;
        }
        if ($canuse == $used) {
            echo json_encode(['success' => false, 'message' => 'error: cdk has been used']);
        } else {
            $sql2 = "UPDATE `cdk` SET used=used+1 WHERE cdk=?";
            $stmt = $conn->prepare($sql2);
            $stmt->bind_param("s", $cdk);
            if ($stmt->execute()) {
                $sql3 = "SELECT vip FROM user WHERE id =?";
                $stmt = $conn->prepare($sql3);
                $stmt->bind_param("s", $userid);
                if ($stmt->execute()) {
                    if ($result = $stmt->get_result()) {
                        $row2 = $result->fetch_assoc();
                        $current_time = new DateTime();
                        $membership_duration_hours = $vips;
                        $vip_expiry_time = new DateTime($row2['vip']);
                        if ($vip_expiry_time < $current_time) {
                            $new_vip_expiry_time = clone $current_time;
                            $new_vip_expiry_time->modify("+{$membership_duration_hours} hours");
                        } else {
                            $new_vip_expiry_time = clone $vip_expiry_time;
                            $new_vip_expiry_time->modify("+{$membership_duration_hours} hours");
                        }
                        $new_vip_expiry_time_str = $new_vip_expiry_time->format('Y-m-d H:i:s');
                        $sql4 = "UPDATE `user` SET vip=? WHERE id=?";
                        $stmt = $conn->prepare($sql4);
                        $stmt->bind_param("ss", $new_vip_expiry_time_str, $userid);
                        if ($stmt->execute()) {
                            echo json_encode(['success' => true, 'message' => 'success: cdk used successfully', 'words' => $words]);
                        } else {
                            echo json_encode(['success' => false, 'message' => 'error: invalid cdk']);
                        }
                    } else {
                        echo json_encode(['success' => false, 'message' => 'error: invalid cdk']);
                    }
                } else {
                    echo json_encode(['success' => false, 'message' => 'error: invalid cdk']);
                }
            } else {
                echo json_encode(['success' => false, 'message' => 'error: cdk has been used']);
            }
        }
    } else {
        echo json_encode(['success' => false, 'message' => 'error: invalid cdk']);
    }
} else {
    echo json_encode(['success' => false, 'message' => 'error: invalid cdk']);
}
$stmt->close();
$conn->close();
